<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class signup_md extends CI_Model{
    private $userTableKeyValue=array("fName"=>"","mInital"=>"","lName"=>"","email"=>"","password"=>"");
    private $userInfoTableKeyValue=array("userBio"=>"","userCity"=>"","userState"=>"",
                                         "userZip"=>"","userInterest"=>"","major"=>"","university"=>"","studentId"=>"","isStudent"=>0);
    function  __construct() {
        parent::__construct();
        //$this->dbInstance=$db;
    }
    function signUpUser($postObj){
        foreach($postObj as $key=>$value){
            if(array_key_exists($key, $this->userTableKeyValue)){
                if($key=="password"){
                    $value=base64_encode($value);
                }
                $this->userTableKeyValue[$key]=$value;
            }else{
                if($key =="isStudent"){
                    if($value=="on"){
                        $this->userInfoTableKeyValue[$key]=1;
                    }
                }else{
                    $this->userInfoTableKeyValue[$key]=$value; 
                }
            } 
        }
        $tranStatus = $this->insertUserData();//it can either return false or an userid
        if($tranStatus!==FALSE){
            //now we need to set session data
            $postObj['userId']=$tranStatus;
            $this->session->set_userdata(array("userData"=>$postObj));
            return true;//meaning session has been save and data was store in the session perfectly
        }else{
            return false; // meaning the data was not save properly
        }
    }

    function insertUserData(){
        //$firstInsertQuery="INSERT INTO user (".implode(",",$this->userTableKey).") VALUES (".implode(",",$uValue).")";
        //$secondInsertQuery = "INSERT INTO userinfo (".implode(",",$this->userInfoTableKey).") VALUES (".implode(",",$uiValue).")";
        $this->db->trans_start();
        $this->db->insert("user",$this->userTableKeyValue);
        $userId= $this->db->insert_id();
        $this->userInfoTableKeyValue["userId"]=$userId;
        $this->db->insert("userinfo",$this->userInfoTableKeyValue);

        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            return FALSE;
        }else{
            $this->db->trans_commit();
            return $userId;
        }
        //$this->db->trans_complete();
        //$secondInsertQuery = "INSERT INTO userinfo (".implode(",",$this->userInfoTableKey).") VALUES (".implode(",",$uiValue).")";
    }

}
?>
